package com.kedsoft.andaily.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.kedsoft.andaily.common.CommonResult;
import com.kedsoft.andaily.entity.Cunsumer;
import com.kedsoft.andaily.entity.CunsumerLogin;
import com.kedsoft.andaily.entity.Router;
import com.kedsoft.andaily.service.CunsumerLoginService;
import com.kedsoft.andaily.service.CunsumerService;
import com.kedsoft.andaily.service.RouterService;
import com.kedsoft.andaily.util.CookiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2019/4/19.
 */
@Api("客户端信息管理")
@RestController
@RequestMapping(value = "/api")
public class CunsumerApiController {

    @Autowired
    RouterService routerService;

    @Autowired
    CunsumerService cunsumerService;

    @Autowired
    CunsumerLoginService cunsumerLoginService;

    @ApiOperation(value="根据ID查询客户端信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "客户端ID"),
    })
    @RequestMapping(value = "/cunsumer/selectById", method = RequestMethod.GET)
    public CommonResult<Cunsumer> getCunsumerById(@RequestParam int id){
        return CommonResult.success(cunsumerService.selectById(id));
    }

    @ApiOperation(value = "获取登录人信息")
    @RequestMapping(value = "/cunsumer/get", method = RequestMethod.GET)
    public CommonResult<Cunsumer> getCunsumer(@CookieValue("userName") String userName)
    {
        return CommonResult.success(cunsumerService.getCunsumerByMobile(userName));
    }

    @ApiOperation(value = "获取巡视单位")
    @RequestMapping(value = "/cunsumer/getRouters", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "routerId", dataType = "int", required = true, value = "巡视单位ID"),
    })
    public CommonResult<List<Router>> getRouters(@RequestParam("routerId") String id)
    {
        //
        if(!id.equals("0")) {
            EntityWrapper<Router> wrapper = new EntityWrapper<>();
            wrapper.eq("id", id);
            wrapper.eq("status", 1);
            return CommonResult.success(routerService.selectList(wrapper));
        }else{
            EntityWrapper<Router> wrapper = new EntityWrapper<>();
            wrapper.eq("status", 1);
            return CommonResult.success(routerService.selectList(wrapper));
        }
    }

    @ApiOperation(value = "获取巡视单位人员信息")
    @RequestMapping(value = "/cunsumer/getByRouterId", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "routerId", dataType = "int", required = true, value = "巡视单位ID"),
    })
    public CommonResult<List<Cunsumer>> getCunsumerByRouterId(@RequestParam("routerId") String id)
    {
        EntityWrapper<Cunsumer> wrapper = new EntityWrapper<>();
        wrapper.eq("routerid", id);
        wrapper.eq("status", 1);
        return CommonResult.success(cunsumerService.selectList(wrapper));
    }

    @ApiOperation(value="客户端登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", dataType = "String", required = true, value = "用户手机号"),
            @ApiImplicitParam(paramType = "query", name = "password", dataType = "String", required = true, value = "用户密码"),
    })
    @RequestMapping(value = "/cunsumer/login", method = RequestMethod.POST)
    public CommonResult<Cunsumer> cunsumerLogin(HttpServletResponse response, @RequestParam String userName, @RequestParam String password){
        EntityWrapper<CunsumerLogin> wrapper = new EntityWrapper<>();
        wrapper.eq("cunsumername",userName);
        wrapper.eq("cunsumerpwd",password);
        CunsumerLogin cunsumerLogin = cunsumerLoginService.selectOne(wrapper);
        if(cunsumerLogin!=null) {
            CookiesUtil.setCookie(response, "userName", userName,0);
            return CommonResult.success(cunsumerService.selectById(cunsumerLogin.getCunsumerid()));
        }else{
            return CommonResult.fail("用户登录失败！");
        }
    }

    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "oldPassword", dataType = "String", required = true, value = "旧密码"),
            @ApiImplicitParam(paramType = "query", name = "newPassword", dataType = "String", required = true, value = "新密码")
    })
    @RequestMapping(value = "/cunsumer/changePassword", method = RequestMethod.POST)
    public CommonResult<String> changePassword(@CookieValue("userName") String userName, @RequestParam String oldPassword, @RequestParam String newPassword)
    {
        System.out.println("userName=" + userName);
        EntityWrapper<CunsumerLogin> wrapper = new EntityWrapper<>();
        wrapper.eq("cunsumername",userName);
        wrapper.eq("cunsumerpwd",oldPassword);
        CunsumerLogin cunsumerLogin = cunsumerLoginService.selectOne(wrapper);
        if(cunsumerLogin != null)
        {
            cunsumerLogin.setCunsumerpwd(newPassword);
            cunsumerLoginService.insertOrUpdate(cunsumerLogin);
            return CommonResult.success("修改密码成功！");
        }
        else
        {
            return CommonResult.fail("旧密码错误！");
        }
    }

}
